package com.gk.http;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.gk.http.entity.HttpCommon;
import com.gk.http.entity.HttpRequestParams;
import com.gk.http.entity.HttpResult;
import okhttp3.*;
import org.springframework.stereotype.Component;

import java.io.IOException;

import static com.gk.http.SSLSocketClientUtil.getUnsafeOkHttpClient;

@Component
public class HttpApi {

    /**
     * http请求
     * 请求1不好用
     *
     * @param httpRequestParams 上传字符串json
     * @return codeData 接口调用返回数据
     */
    public static String HttpRequest(HttpRequestParams httpRequestParams, String urlBuffer) {
        OKHttpUtilForSelfSSL http = OKHttpUtilForSelfSSL.Builder();
        String url = HttpCommon.URL + urlBuffer;
        String codeData = "";
        try {
            codeData = http.postJsonParams
                    (url, JSON.toJSONString(httpRequestParams));
        } catch (Exception e) {

        }
        return codeData;
    }

    /**
     * http请求
     * 请求2 好用
     *
     * @param httpRequestParams 上传字符串json
     * @return codeData 接口调用返回数据
     */
    public static HttpResult HttpRequest2(HttpRequestParams httpRequestParams, String urlBuffer) {
        String url = HttpCommon.URL + urlBuffer;
        OkHttpClient okHttpClient = new OkHttpClient();
        MediaType _JSON = MediaType.parse("application/json; charset=utf-8");
        RequestBody body = RequestBody.create(_JSON, com.alibaba.fastjson.JSON.toJSONString(httpRequestParams));
        Request request = new Request.Builder()
                .header("Authorization", HttpCommon.Authorization)
                .header("KeyId", HttpCommon.KeyId)
                .url(url)
                .post(body)
                .build();
        Call call = okHttpClient.newCall(request);
        try {
//            Response response = call.execute();
//            String httpRsult = response.body().string();

            Response response = getUnsafeOkHttpClient().newCall(request).execute();
            String httpRsult = response.body().string();
            System.out.println("请求国药网络返回数据>>>>" + response);
            return JSONObject.parseObject(httpRsult, HttpResult.class);
//            return  new HttpResult("-999","千方服务器请求国药网络传输异常",null);
        } catch (IOException e) {
            e.printStackTrace();
            return new HttpResult("-999", "千方服务器请求国药网络传输异常", null);
        }

    }


    public static HttpResult getDataHttpApi(HttpRequestParams httpRequestParams, String urlBuffer) {
        String url = HttpCommon.URL + urlBuffer;
        OkHttpClient okHttpClient = new OkHttpClient();
        MediaType _JSON = MediaType.parse("application/json; charset=utf-8");
        RequestBody body = RequestBody.create(_JSON, com.alibaba.fastjson.JSON.toJSONString(httpRequestParams));
        Request request = new Request.Builder()
                .header("Authorization", HttpCommon.Authorization)
                .header("KeyId", HttpCommon.KeyId)
                .url(url)
                .post(body)
                .build();
        Call call = okHttpClient.newCall(request);
        try {
            Response response = getUnsafeOkHttpClient().newCall(request).execute();
//            System.out.println(response.body().string());
            String httpRsult = response.body().string();
            System.out.println("请求国药网络返回数据>>>>" + httpRsult);
            return JSONObject.parseObject(httpRsult, HttpResult.class);
//            return  new HttpResult("-999","千方服务器请求国药网络传输异常",null);
        } catch (IOException e) {
            e.printStackTrace();
            return new HttpResult("-999", "千方服务器请求国药网络传输异常", null);
        }

    }
}
